package com.stock.controller;



import com.stock.dto.BatchDecreaseStock;
import com.stock.pojo.StockWarningConfig;
import com.stock.service.StockService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/stock")
@RefreshScope // 动态配置刷新
public class StockController {

    private final StockService stockService;

    public StockController(StockService stockService) {
        this.stockService = stockService;
    }

    // 扣减库存接口，处理异常并返回合适的错误提示信息
    @PostMapping("/decrease")
    public ResponseEntity<String> decreaseStock(@RequestParam String productId, @RequestParam int quantity) {
        try {
            stockService.decreaseStock(productId, quantity);
            return new ResponseEntity<>("库存扣减成功", HttpStatus.OK);
        } catch (IllegalArgumentException e) {
            return new ResponseEntity<>("库存扣减失败: " + e.getMessage(), HttpStatus.BAD_REQUEST);
        }
    }

    // 增加库存接口，返回操作结果信息
    @PostMapping("/increase")
    public ResponseEntity<String> increaseStock(@RequestParam String productId, @RequestParam int quantity) {
        stockService.increaseStock(productId, quantity);
        return new ResponseEntity<>("库存增加成功", HttpStatus.OK);
    }

    // 获取商品库存数量接口，返回库存数量
    @GetMapping("/quantity/{productId}")
    public ResponseEntity<Integer> getStockQuantity(@PathVariable String productId) {
        int quantity = stockService.getStockQuantity(productId);
        return new ResponseEntity<>(quantity, HttpStatus.OK);
    }

    // 设置库存预警阈值接口，返回操作结果信息
    @PutMapping("/warning-threshold")
    public ResponseEntity<String> setStockWarningThreshold(@RequestParam String productId, @RequestParam int threshold) {
        stockService.setStockWarningThreshold(productId, threshold);
        return new ResponseEntity<>("库存预警阈值设置成功", HttpStatus.OK);
    }

    // 判断是否低于库存预警接口，返回判断结果
    @GetMapping("/below-warning/{productId}")
    public ResponseEntity<Boolean> isBelowStockWarning(@PathVariable String productId) {
        boolean result = stockService.isBelowStockWarning(productId);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    // 批量扣减库存接口，处理异常并返回合适的提示信息
    @PostMapping("/batch-decrease")
    public ResponseEntity<String> batchDecreaseStock(@RequestBody BatchDecreaseStock batchDecreaseStock) {
        try {
            stockService.batchDecreaseStock(batchDecreaseStock.getProductIds(), batchDecreaseStock.getQuantities());
            return new ResponseEntity<>("批量库存扣减成功", HttpStatus.OK);
        } catch (IllegalArgumentException e) {
            return new ResponseEntity<>("批量库存扣减失败: " + e.getMessage(), HttpStatus.BAD_REQUEST);
        }
    }

    @Value("${url}")
    private String url;

    @GetMapping("/queryUrl")
    public String getUrl(){
        System.out.println("url = " + url);
        return url;
    }

    // 获取多个商品的库存预警配置信息接口，返回配置信息列表
    @GetMapping("/warning-configs")
    public ResponseEntity<List<StockWarningConfig>> getStockWarningConfigs(@RequestBody List<String> productIds) {
        List<StockWarningConfig> configs = stockService.getStockWarningConfigs(productIds);
        return new ResponseEntity<>(configs, HttpStatus.OK);
    }
}